<?php

/**
 * Maikuro Framework
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/**
 * The Utility class contains general utility methods.
 */
class Utility
{
    /**
     * Overwrites $array1's values with $array2's (recursively).
     *
     * @return array
     * @param array $array1
     * @param array $array2
     */
    public static function arrayOverlay(array $array1, array $array2)
    {
        foreach($array1 as $key => $value) {
            if(!array_key_exists($key, $array2)) {
                continue;
            }
            if(is_array($value) && is_array($array2[$key])){
                $array1[$key] = self::arrayOverlay($value, $array2[$key]);
            } else {
                $array1[$key] = $array2[$key];
            }
        }
        return $array1;
    }

    /**
     * Applies any filters to $array where $filters[$key] matches $array[$key].
     *
     * @param array $array
     * @param array $filters
     * @return array
     */
    public static function filterArray(array &$array, array $filters)
    {
        /**
         * @see Filters
         */
        require 'Filters.php';
        
        foreach ($array as $key => $value) {
            if (!isset($filters[$key]) || !is_array($filters[$key])
                || !count($filters[$key])) {
                    continue;
            }
            foreach ($filters[$key] as $filterKey => $filterValue) {
                if (is_int($filterKey)) {
                    $filter = $filterValue;
                    $result = Filters::$filter($value);
                } else if (is_array($filterValue)) {
                    $filter = $filterKey;
                    $filterParams = array_unshift($filterValue, $value);
                    $result = call_user_func_array(array('Filters', $filter), $filterParams);
                } else {
                    $filter = $filterKey;
                    $result = Filters::$filter($value, $filterValue);
                }
            }
            $array[$key] = $result;
        }
        return $array;
    }

    /**
     * Checks whether an array is associative or not.
     *
     * @param array $array
     * @return boolean
     */
    public static function isArrayAssociative(array $array)
    {
        return (boolean) array_diff_key($array, array_keys(array_keys($array)));
    }
}